
/*
 * ArchiverGUI.java
 *
 * Created on 09 28, 13, 12:10:30 AM
 */


/**
 *
 * @author Tangangco, Job L.
 */
import javax.swing.*;
import java.io.*;

public class ArchiverGUI extends javax.swing.JFrame {


    private JFileChooser fc = new JFileChooser();
    private UncompressedFile u;
    private UncompressedFile u2;
    private File file;
    private File file2;
    private static final String OUTPUT_FOLDER = "D:/Pandora's Extracts";
    private static final String ZIP_FOLDERS = "D:/Pandora's Zips/";
    private UnZip unzip = new UnZip();

    /** Creates new form ArchiverGUI */

    public ArchiverGUI() {
        initComponents();
        u = new UncompressedFile(zipName.getText());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {


        jLabel1 = new javax.swing.JLabel();
        addToCompress = new javax.swing.JButton();
        compressButton = new javax.swing.JButton();
        decompressButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        log = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        zipName = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Monospaced", 0, 36));
        jLabel1.setText("Pandora's Archiver");

        addToCompress.setFont(new java.awt.Font("Dialog", 0, 14));
        addToCompress.setText("Add Files to Compress");
        addToCompress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToCompressActionPerformed(evt);
            }
        });

        compressButton.setFont(new java.awt.Font("Dialog", 0, 14));
        compressButton.setText("Compress Files");
        compressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compressButtonActionPerformed(evt);
            }
        });

        decompressButton.setFont(new java.awt.Font("Dialog", 0, 14));
        decompressButton.setText("Decompress Files");
        decompressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                decompressButtonActionPerformed(evt);
            }
        });

        log.setColumns(20);
        log.setEditable(false);
        log.setRows(5);
        jScrollPane1.setViewportView(log);

        jLabel2.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
        jLabel2.setText("Compressed File Name:");

        zipName.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
        zipName.setText("Pandora Zip.zip");
        zipName.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(decompressButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(compressButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(addToCompress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(zipName, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(54, 54, 54))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                            .addComponent(zipName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(addToCompress, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(compressButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(decompressButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>

/**
*Accepts an action event(evt) which allows the user to select files to be compressed using a File Chooser.
*Chosen files are temporarily stored until the "Compress Now" button is clicked.
*@param evt
*/
private void addToCompressActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = fc.showOpenDialog(ArchiverGUI.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
         file = fc.getSelectedFile();
         try{
             u.addFile(new BinaryFileData(file.getPath(), ZipFileUtil.fileToBytes(file.getPath())));
             System.out.println(file);
         }catch(IOException ee){
             ee.printStackTrace();
         }
         log.append("Add to Compression: " + file.getName() + ".\n");
         } else {
              log.append("File choosing cancelled by user. \n");
         }
         log.setCaretPosition(log.getDocument().getLength());
}
/**
*Accepts an action event(evt) which compresses all files added for compression.
*The .zip file will be stored in the same directory as ArchiverGUI.java
*@param evt
*/
private void compressButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:

	try{
            byte[] b=ZipFileUtil.compress(u);
            ZipFileUtil.byteArrayToFile(u.getPath(),b);
            log.append("Succesfully compressed "+ zipName.getText()+"\n");
    }catch(IOException ie){
            ie.printStackTrace();
       }
}

/**
*Accepts an action event(evt) which loads a File Chooser to allow the user to choose
*a .zip file to be decompressed. The contents will be stored in the same directory
*as ArchiverGUI.java
*@param evt
*/
private void decompressButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
	fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = fc.showOpenDialog(ArchiverGUI.this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    	file2 = fc.getSelectedFile();
		unzip.unZipIt(file2.toString(),OUTPUT_FOLDER);
		log.append("Succesfully decompressed file "+ file2.getName()+"\n");
    }
}

    /**
     * @param args the command line arguments
     */

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ArchiverGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ArchiverGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ArchiverGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ArchiverGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ArchiverGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton addToCompress;
    private javax.swing.JButton compressButton;
    private javax.swing.JButton decompressButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea log;
    private javax.swing.JTextField zipName;
    // End of variables declaration
}
